package com.fenghen.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.fenghen.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.fenghen.system.domain.RiskIdentification;
import com.fenghen.system.mapper.RiskAssessmentMapper;
import com.fenghen.system.domain.RiskAssessment;
import com.fenghen.system.service.IRiskAssessmentService;
import com.fenghen.common.core.text.Convert;

/**
 * 风险评估与评级模块Service业务层处理
 * 
 * @author fenghen
 * @date 2024-11-06
 */
@Service
public class RiskAssessmentServiceImpl implements IRiskAssessmentService 
{
    @Autowired
    private RiskAssessmentMapper riskAssessmentMapper;

    /**
     * 查询风险评估与评级模块
     * 
     * @param assessmentId 风险评估与评级模块主键
     * @return 风险评估与评级模块
     */
    @Override
    public RiskAssessment selectRiskAssessmentByAssessmentId(Long assessmentId)
    {
        return riskAssessmentMapper.selectRiskAssessmentByAssessmentId(assessmentId);
    }

    /**
     * 查询风险评估与评级模块列表
     * 
     * @param riskAssessment 风险评估与评级模块
     * @return 风险评估与评级模块
     */
    @Override
    public List<RiskAssessment> selectRiskAssessmentList(RiskAssessment riskAssessment)
    {
        return riskAssessmentMapper.selectRiskAssessmentList(riskAssessment);
    }

    /**
     * 新增风险评估与评级模块
     * 
     * @param riskAssessment 风险评估与评级模块
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRiskAssessment(RiskAssessment riskAssessment)
    {
        int rows = riskAssessmentMapper.insertRiskAssessment(riskAssessment);
        insertRiskIdentification(riskAssessment);
        return rows;
    }

    /**
     * 修改风险评估与评级模块
     * 
     * @param riskAssessment 风险评估与评级模块
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRiskAssessment(RiskAssessment riskAssessment)
    {
        riskAssessmentMapper.deleteRiskIdentificationByRiskId(riskAssessment.getAssessmentId());
        insertRiskIdentification(riskAssessment);
        return riskAssessmentMapper.updateRiskAssessment(riskAssessment);
    }

    /**
     * 批量删除风险评估与评级模块
     * 
     * @param assessmentIds 需要删除的风险评估与评级模块主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskAssessmentByAssessmentIds(String assessmentIds)
    {
        riskAssessmentMapper.deleteRiskIdentificationByRiskIds(Convert.toStrArray(assessmentIds));
        return riskAssessmentMapper.deleteRiskAssessmentByAssessmentIds(Convert.toStrArray(assessmentIds));
    }

    /**
     * 删除风险评估与评级模块信息
     * 
     * @param assessmentId 风险评估与评级模块主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskAssessmentByAssessmentId(Long assessmentId)
    {
        riskAssessmentMapper.deleteRiskIdentificationByRiskId(assessmentId);
        return riskAssessmentMapper.deleteRiskAssessmentByAssessmentId(assessmentId);
    }

    /**
     * 新增风险识别模块信息
     * 
     * @param riskAssessment 风险评估与评级模块对象
     */
    public void insertRiskIdentification(RiskAssessment riskAssessment)
    {
        List<RiskIdentification> riskIdentificationList = riskAssessment.getRiskIdentificationList();
        Long assessmentId = riskAssessment.getAssessmentId();
        if (StringUtils.isNotNull(riskIdentificationList))
        {
            List<RiskIdentification> list = new ArrayList<RiskIdentification>();
            for (RiskIdentification riskIdentification : riskIdentificationList)
            {
                riskIdentification.setRiskId(assessmentId);
                list.add(riskIdentification);
            }
            if (list.size() > 0)
            {
                riskAssessmentMapper.batchRiskIdentification(list);
            }
        }
    }
}
